package com.syni.mdd.yhd.common.all.service.impl;

import com.syni.mdd.yhd.common.all.constants.Constants;
import com.syni.mdd.yhd.common.all.constants.ResultMsg;
import com.syni.mdd.yhd.common.all.dao.jpa.BmsMarketActivityDao;
import com.syni.mdd.yhd.common.all.dto.requestDto.BaseReDto;
import com.syni.mdd.yhd.common.all.dto.requestDto.marketing.MarketActivityReDto;
import com.syni.mdd.yhd.common.all.entity.BmsMarketActivity;
import com.syni.mdd.yhd.common.all.service.BmsCommonService;
import com.syni.mdd.yhd.common.all.service.BmsMarketActivityService;
import com.syni.mdd.sjb.common.component.utils.common.utils.ParamsMap;
import com.syni.mdd.sjb.common.component.utils.common.utils.ResultMapHelper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.Optional;

/**
 * @className
 * @description TOO
 * @Author cfx
 * @DATE 2019/4/9 12:04
 * @VERSION 1.0
 **/
@Service
public class BmsMarketActivityImpl extends BaseService<BmsMarketActivity, Integer> implements BmsMarketActivityService {

    @Autowired
    BmsMarketActivityDao bmsMarketActivityDao;

    @Override
    public BmsMarketActivityDao getBaseDao() {
        return bmsMarketActivityDao;
    }

    @Override
    public Map<String, Object> releaseActivity(MarketActivityReDto activityReDto) {
        BmsMarketActivity bmsMarketActivity = new BmsMarketActivity();
        BeanUtils.copyProperties(activityReDto,bmsMarketActivity);
        bmsMarketActivity.setIsDelete(Constants.COMMON_FLAG_FALSE_INT);
        bmsMarketActivityDao.save(bmsMarketActivity);
        return ResultMapHelper.success(bmsMarketActivity, ResultMsg.HAND_SUCCESS);
    }

    @Override
    public Map<String, Object> findActivity(BaseReDto baseReDto) {
        Optional<BmsMarketActivity> bmsMarketActivityOptional = bmsMarketActivityDao.findById(baseReDto.getId());
        if(bmsMarketActivityOptional.isPresent()){
            BmsMarketActivity bmsMarketActivity = bmsMarketActivityOptional.get();
            return ResultMapHelper.success(bmsMarketActivity,ResultMsg.MSG_GET_SUCCESS);
        }
        return ResultMapHelper.failure("活动不存在");
    }

    @Override
    public BmsMarketActivity findById(Integer activityId) {
        Optional<BmsMarketActivity> bmsMarketActivity = bmsMarketActivityDao.findById(activityId);
        if(bmsMarketActivity.isPresent()){
            return bmsMarketActivity.get();
        }
        return null;
    }

    @Override
    public Map<String, Object> activityList(ParamsMap paramsMap) {
        Integer businessId = paramsMap.getInteger("businessId");
        Integer pageNum = paramsMap.getInteger("pageNum");
        Integer pageSize = paramsMap.getInteger("pageSize");
        Pageable pageable = BmsCommonService.getPageable(pageNum,pageSize, Sort.Direction.DESC,"newTime");
        //查询活动
        List<BmsMarketActivity> bmsMarketActivityList = bmsMarketActivityDao.findByBusinessId(businessId,pageable);
        return ResultMapHelper.success(bmsMarketActivityList, ResultMsg.MSG_GET_SUCCESS);
    }

    @Override
    public boolean checkIsHavingActivity(Integer businessId) {
        List<BmsMarketActivity> bmsMarketActivities = bmsMarketActivityDao.findByBusinessId(businessId);
        if(bmsMarketActivities != null && bmsMarketActivities.size() > 0){
            return true;
        }
        return false;
    }
}
